import clone from 'clone'

class Compiler {
        static defineContainer = [
                "list",
                "columns",
                "container",
                "hiddenColumns",
                "handles",
        ];
        mapping = {
                'sinput': 'minput',
                'sbutton': 'mbutton',
        }
        mobileRoot = {}
        constructor() { }
        findContainers(item) {
                let containers = Object.keys(item).filter(function (v) {
                        return Compiler.defineContainer.indexOf(v) > -1;
                });
                return containers;
        }
        findType(unit) {
                if (unit.type && unit.model) {
                        return unit.type.slice(1);
                }

        }
        Uppercase(str) {
                return str && str.charAt(0).toUpperCase() + str.slice(1)
        }
        pcToMobile(root) {
                this.recursive( clone(root) , this.mobileRoot)
                console.log('this.mobileRoot', this.mobileRoot)
                return this.mobileRoot
        }
        recursive(unit, mobileComponent) {
                let turnType = this.Uppercase(this.findType(unit))
                if (turnType && this[`turnMobile${turnType}`]) {
                        this[`turnMobile${turnType}`](unit, mobileComponent)
                }
                let containers = this.findContainers(unit);
                containers.map((containerName) => {
                        mobileComponent[containerName] = []
                        unit[containerName] && unit[containerName].map((item, index) => {
                                this.recursive(item, mobileComponent[containerName][index] = {})
                        })
                })
        }
        turnMobileInput(unit, mobileComponent) {

                mobileComponent.type = 'minput'
                mobileComponent.name = '文本框'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = {
                        defaultValue: unit.options.defaultValue,
                        hidden: unit.options.hidden,
                        clearable: unit.options.clearable,
                        disabled: unit.options.disabled,
                        readonly: unit.options.readonly,
                        label: unit.options.label,
                        fieldType: unit.options.fieldType,
                        fieldLength: unit.options.fieldLength,
                        placeholder: unit.options.placeholder,
                        regularCheck: unit.options.regularCheck,
                        selfRuleCheck: unit.options.selfRuleCheck,
                        triggerType: unit.options.triggerType,
                        eventCode: unit.options.eventCode,
                        sourceCode: unit.options.sourceCode,
                        sourceBingfield: unit.options.sourceBingfield,
                }

        }
        turnMobileSubmeter(unit, mobileComponent) {
                mobileComponent.type = 'msubmeter'
                mobileComponent.name = '分表容器'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = unit.options
        }
        turnMobileBlank(unit, mobileComponent) {
                mobileComponent.type = 'mblank'
                mobileComponent.name = '空白容器'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = unit.options
                mobileComponent.columns = unit.columns

        }
        turnMobileLayout(unit, mobileComponent) {
                mobileComponent.type = 'mblank'
                mobileComponent.name = '空白容器'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = {
                        className: '',
                        hidden: false,
                        backgroundColor: '',
                        borderRadius: 0,
                        marginTop: 0,
                        marginRight: 0,
                        marginBottom: 0,
                        marginLeft: 0,
                      }
                let columns = []
                unit.columns.map(item => {
                       columns=[...columns,...item['list']]
                })
                mobileComponent.columns = columns
                unit.columns = columns

        }
        turnMobileOrdinary_panel(unit, mobileComponent){
                mobileComponent.type = 'mblank'
                mobileComponent.name = '空白容器'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = {
                        className: '',
                        hidden: false,
                        backgroundColor: '',
                        borderRadius: 0,
                        marginTop: 0,
                        marginRight: 0,
                        marginBottom: 0,
                        marginLeft: 0,
                      }
                mobileComponent.columns = unit.columns
        }
        turnMobileCard(unit, mobileComponent){
                mobileComponent.type = 'mblank'
                mobileComponent.name = '空白容器'
                mobileComponent.key = unit.key
                mobileComponent.model = unit.model
                mobileComponent.icon = 'icon-shurukuang'
                mobileComponent.options = {
                        className: '',
                        hidden: false,
                        backgroundColor: '',
                        borderRadius: 0,
                        marginTop: 0,
                        marginRight: 0,
                        marginBottom: 0,
                        marginLeft: 0,
                      }
                 mobileComponent.columns = unit.container
                 unit.columns = unit.container
        }

}

export const compiler = new Compiler()